(function($){
    var zp = {
        init: function (obj, pageInit) {
            return (function () {
                zp.addhtml(obj, pageInit);
                zp.bindEvent(obj, pageInit);
            }());
        },
        addhtml: function(obj, pageInit) {
            var start, end;
            return(function(){
                obj.empty();
                if(pageInit.current>1){
                    obj.append('<a href="javascript:;" class="prebtn">上一页</a>');
                }else{
                    obj.remove('.prevPage');
                    obj.append('<span class="disabled">上一页</span>');
                }

                if(pageInit.current>4 && pageInit.pageNum>4){

                    obj.append('<a href="javascript:;" class="zxfPagenum">'+1+'</a>');

                    obj.append('<a href="javascript:;" class="zxfPagenum">'+2+'</a>');

                    obj.append('<span>...</span>');
                }

                if (pageInit.current > 4 && pageInit.current <= pageInit.pageNum - 5) {
                    start = pageInit.current - 2;
                    end = pageInit.current + 2;
                } else if (pageInit.current > 4 && pageInit.current > pageInit.pageNum - 5) {
                    start = pageInit.pageNum - 4;
                    end = pageInit.pageNum;
                } else {
                    start = 1;
                    end = 9;
                }

                for(; start<= end; start++) {
                    if(start <= pageInit.pageNum && start >= 1) {
                        if( pageInit.current>4 && start <=2) {
                            continue;
                        }
                        if (start == pageInit.current) {
                            obj.append('<span class="current">' + start + '</span>');
                        } else if (start == pageInit.current + 1) {
                            obj.append('<a href="javascript:;" class="zxfPagenum nextpage">'+start+'</a>');
                        } else {
                            obj.append('<a href="javascript:;" class="zxfPagenum">' + start + '</a>');
                        }
                    }
                }

                if(end<pageInit.pageNum){
                    obj.append('<span>...</span>');
                }

                if(pageInit.current>=pageInit.pageNum){
                    obj.remove('.nextbtn');
                    obj.append('<span class="disabled">下一页</span>');
                }else{obj.append('<a href="javascript:;" class="nextbtn">下一页</a>');
                }
                obj.append('<span>'+'共'+'<b>'+pageinit.pageNum+'</b>'+'页，'+'</span>');
                obj.append('<span>'+'到第'+'<input type="number" class="zxfinput" />'+'页'+'</span>');
                obj.append('<span class="zxfokbtn">'+'确定'+'</span>');}());
        },

        bindEvent:function(obj,pageInit){
            return(function(){
                obj.off('click','a.prebtn').on('click','a.prebtn',function(){
                    var cur=parseInt(obj.children('span.current').text());
                    var current=$.extend(pageInit,{'current':cur-1});
                    zp.addhtml(obj,current);
                    if(typeof(pageInit.backfun)=='function'){
                        pageInit.backfun(current);
                    }
                });

                obj.off('click','a.zxfPagenum').on('click','a.zxfPagenum',function(){
                    var cur=parseInt($(this).text());
                    var current=$.extend(pageInit,{'current':cur});
                    zp.addhtml(obj,current);
                    if(typeof(pageInit.backfun)=='function'){
                        pageInit.backfun(current);
                    }
                });

                obj.off('click','a.nextbtn').on('click','a.nextbtn',function(){
                    var cur=parseInt(obj.children('span.current').text());
                    var current=$.extend(pageInit,{'current':cur+1});
                    zp.addhtml(obj,current);

                    if(typeof(pageInit.backfun)=='function'){
                        pageInit.backfun(current);
                    }
                });

                obj.off('click','span.zxfokbtn').on('click','span.zxfokbtn',function(){
                    if($('input.zxfinput').val()>pageInit.pageNum||$('input.zxfinput').val()<1){
                      alert('您输入的页码不存在，请重新输入。');
                      return;
                    }
                    var cur=parseInt(obj.find('input.zxfinput').val());
                    var current=$.extend(pageInit,{'current':cur});
                    zp.addhtml(obj,{'current':cur,'pageNum':pageInit.pageNum});
                    if(typeof(pageInit.backfun)=='function'){
                        pageInit.backfun(current);
                        $('input.zxfinput').val(cur)
                    }
                });
            }());
        }
    };
    $.fn.createPage=function(options){
        var pageInit=$.extend({pageNum:15,current:1,backfun:function(){}},options);
        zp.init(this,pageInit);}
    }(jQuery));
